package top.xiaoshuaila.moments.mapper;

import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import top.xiaoshuaila.moments.pojo.Dynamic;

import java.util.List;

@Repository
public interface FindMapper {

    @Select("select \n" +
            "  a.`id` id,\n" +
            "  a.`userid` userid,\n" +
            "  a.`time` time,\n" +
            "  a.`iszhuan` iszhuan,\n" +
            "  a.`dyid` dyid,\n" +
            "  a.`selfonly` selfonly,\n" +
            "  b.`text` text,\n" +
            "  b.`title` title,\n" +
            "  b.`image` image,\n" +
            "  b.`userid` dyuserid,\n" +
            "  c.num zan,\n" +
            "  d.zhuan zhuan,\n" +
            "  e.`realname` realname,\n" +
            "  f.headphoto headphoto,\n" +
            "  f.realname username,\n" +
            "  (select count(*)>0 from zan where zan.userid=${id} and zan.dyid=a.id) iszan\n" +
            "from\n" +
            "  userdy a \n" +
            "  left join dynamic b \n" +
            "    on a.`dyid` = b.`id` \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      COUNT(*) as num \n" +
            "    from\n" +
            "      zan \n" +
            "    group by zan.`dyid`) c \n" +
            "    on a.id = c.dyid \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      COUNT(*) - 1 as zhuan \n" +
            "    from\n" +
            "      userdy \n" +
            "    group by userdy.`dyid`) d \n" +
            "    on b.id = d.dyid \n" +
            "  left join users e \n" +
            "    on b.`userid` = e.`id` \n" +
            "  left join users f \n" +
            "    on a.userid = f.`id` \n" +
            "where a.userid = ${id} \n" +
            "  or (\n" +
            "    a.userid != ${id} \n" +
            "    and a.userid in \n" +
            "    (select \n" +
            "      focus.userid \n" +
            "    from\n" +
            "      focus \n" +
            "    where focus.followerid = ${id} \n" +
            "      and a.selfonly = 0)\n" +
            "  )order by a.time desc limit ${page},${num}")
    List<Dynamic> getfindfynamic(long id,int page,int num);

    @Select("select \n" +
            "  count(*)\n" +
            "from\n" +
            "  userdy a\n" +
            "where a.userid = ${id} \n" +
            "  or (\n" +
            "    a.userid != ${id} \n" +
            "    and a.userid in \n" +
            "    (select \n" +
            "      focus.userid \n" +
            "    from\n" +
            "      focus \n" +
            "    where focus.followerid = ${id} \n" +
            "      and a.selfonly = 0)\n" +
            "  )")
    int gettotal(long id);
    @Select("select \n" +
            "  count(*)\n" +
            "from\n" +
            "  userdy a\n" +
            "where a.userid = ${id} \n" +
            "  or a.selfonly = 0" )
    int gettotalsquare(long id);

    @Select("select \n" +
            "  a.`id` id,\n" +
            "  a.`userid` userid,\n" +
            "  a.`time` time,\n" +
            "  a.`iszhuan` iszhuan,\n" +
            "  a.`dyid` dyid,\n" +
            "  a.`selfonly` selfonly,\n" +
            "  b.`text` text,\n" +
            "  b.`title` title,\n" +
            "  b.`image` image,\n" +
            "  b.`userid` dyuserid,\n" +
            "  c.num zan,\n" +
            "  d.zhuan zhuan,\n" +
            "  e.`realname` realname,\n" +
            "  f.headphoto headphoto,\n" +
            "  f.realname username,\n" +
            "  (select count(*)>0 from zan where zan.userid=${id} and zan.dyid=a.id) iszan\n" +
            "from\n" +
            "  userdy a \n" +
            "  left join dynamic b \n" +
            "    on a.`dyid` = b.`id` \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      COUNT(*) as num \n" +
            "    from\n" +
            "      zan \n" +
            "    group by zan.`dyid`) c \n" +
            "    on a.id = c.dyid \n" +
            "  left join \n" +
            "    (select \n" +
            "      dyid,\n" +
            "      COUNT(*) - 1 as zhuan \n" +
            "    from\n" +
            "      userdy \n" +
            "    group by userdy.`dyid`) d \n" +
            "    on b.id = d.dyid \n" +
            "  left join users e \n" +
            "    on b.`userid` = e.`id` \n" +
            "  left join users f \n" +
            "    on a.userid = f.`id` \n" +
            "where a.userid = ${id} \n" +
            "  or  a.selfonly = 0 order by a.time desc limit ${page},${num}")
    List<Dynamic> getsquarefynamic(long id,int page,int num);
}
